package com.ruoyi.system.https.bo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.ruoyi.system.domain.PwsTable;
import lombok.Data;
import lombok.ToString;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
@Data
@Log4j2
@JacksonXmlRootElement(localName = "Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
@ToString
public class ZMES_GET_PWOH_REPONSE {
    @JacksonXmlProperty(localName = "Body", namespace = "http://schemas.xmlsoap.org/soap/envelope/")
    private ZWS_PP_GET_PWOH_Body body;

    public ZWS_PP_GET_PWOH_Body getBody() {
        return body;
    }

    public void setBody(ZWS_PP_GET_PWOH_Body body) {
        this.body = body;
    }
    @ToString
    public static class ZWS_PP_GET_PWOH_Body{
        @JacksonXmlProperty(localName = "ZmesGetPwohResponse", namespace = "urn:sap-com:document:sap:soap:functions:mc-style")
        private ZmesGetPwohResponse zmesGetPwohResponse;
        public ZmesGetPwohResponse getZmesGetPwohResponse() {
            return zmesGetPwohResponse;
        }

        public void setZmesGetPwohResponse(ZmesGetPwohResponse zmesGetPwohResponse) {
            this.zmesGetPwohResponse = zmesGetPwohResponse;
        }
    }
    @ToString
    public static class ZmesGetPwohResponse {

        @JacksonXmlProperty(localName = "Pwoh")
        private Pwoh pwoh;

        // Getters and Setters
        public Pwoh getPwoh() {
            return pwoh;
        }

        public void setPwoh(Pwoh pwoh) {
            this.pwoh = pwoh;
        }
    }
    @ToString
    public static class Pwoh {

        @JacksonXmlElementWrapper(useWrapping = false)
        @JacksonXmlProperty(localName = "item")
        private List<Item> items;

        // Getters and Setters
        public List<Item> getItems() {
            return items;
        }

        public void setItems(List<Item> items) {
            this.items = items;
        }
    }
    @Data
    @ToString
    public static class Item {

        @JacksonXmlProperty(localName = "PwoNo")
        private String pwoNo;

        @JacksonXmlProperty(localName = "OrderType")
        private String orderType;

        @JacksonXmlProperty(localName = "Material")
        private String material;

        @JacksonXmlProperty(localName = "Description")
        private String description;

        @JacksonXmlProperty(localName = "Batch")
        private String batch;

        @JacksonXmlProperty(localName = "Quantity")
        private double quantity;

        @JacksonXmlProperty(localName = "StartDate")
        private String startDate;

        @JacksonXmlProperty(localName = "EndDate")
        private String endDate;

        @JacksonXmlProperty(localName = "ProdVer")
        private String prodVer;

        @JacksonXmlProperty(localName = "RecipeNo")
        private String recipeNo;

        @JacksonXmlProperty(localName = "AltBom")
        private String altBom;

        @JacksonXmlProperty(localName = "LastBatchNo")
        private String lastBatchNo;

        @JacksonXmlProperty(localName = "LastBatchDate")
        private String lastBatchDate;

        @JacksonXmlProperty(localName = "LastProdVer")
        private String lastProdVer;

        @JacksonXmlProperty(localName = "Label")
        private String label;


    }
    public  static  List<PwsTable> bodyToPwsTable(ZMES_GET_PWOH_REPONSE zmes_get_pwoh_reponse){
        List<PwsTable> pwsTables=new ArrayList<>();
        for (Item aItem:zmes_get_pwoh_reponse.getBody().getZmesGetPwohResponse().getPwoh().getItems()) {
            if (StringUtils.isNotBlank(aItem.getPwoNo() )&&(aItem.getOrderType().equals("ZWSF")||aItem.getOrderType().equals("ZWFG"))){
                PwsTable pwsTable = new PwsTable();
                pwsTable.setOrderNo(aItem.getPwoNo());
                pwsTable.setOrderType(aItem.getOrderType());
                pwsTable.setMatlNo(aItem.getMaterial());
                pwsTable.setMatlDes(aItem.getDescription().replaceAll("'", ""));//去除单引号
                pwsTable.setBatchNo(aItem.getBatch());
                pwsTable.setQty(new BigDecimal(aItem.getQuantity()));
                if(aItem.getStartDate().length()==10){
                    aItem.setStartDate(aItem.getStartDate() + "T00:00:00");
                }
                try {
                    pwsTable.setOrderStartDate(LocalDateTime.parse(aItem.getStartDate()));
                }catch (Exception e){
                    log.error("{}:{}",aItem.getStartDate(),e.getMessage());
                }
                if(aItem.getEndDate().length()==10){
                    aItem.setEndDate(aItem.getEndDate() + "T00:00:00");
                }
               try {
                   pwsTable.setOrderEndDate(LocalDateTime.parse(aItem.getEndDate()));
               }catch (Exception e){
                   log.error("{}:{}",aItem.getEndDate(),e.getMessage());
               }
                pwsTable.setProductionVersion(aItem.getProdVer());
                pwsTable.setRecipeNo(aItem.getRecipeNo());
                pwsTable.setBomAlt(aItem.getAltBom());
                pwsTable.setLastbatchNo(aItem.getLastBatchNo());
                pwsTable.setCreateTime(LocalDateTime.now());
                pwsTable.setUpdateTime(LocalDateTime.now());
                if(aItem.getLastBatchDate().length()==10){
                    try {
                        String date = aItem.getLastBatchDate() + "T00:00:00";
                        pwsTable.setLastbatchDate(LocalDateTime.parse(date));
                    }catch (Exception e){
                        log.error("{}:{}",aItem.getLastBatchDate(),e.getMessage());
                    }
                }else if(aItem.getLastBatchDate().length()==19) {
                    try {
                        pwsTable.setLastbatchDate(LocalDateTime.parse(aItem.getLastBatchDate()));
                    }catch (Exception e){
                        log.error("{}:{}",aItem.getLastBatchDate(),e.getMessage());
                    }
                }
                pwsTable.setLastprodVer(aItem.getLastProdVer());
                pwsTable.setIsDel(0);
                pwsTables.add(pwsTable);
            }
        }
        return pwsTables;
    }

}
